﻿using Furion.DataValidation;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace FurStudy.Application
{
    public class TestDto
    {
        [Range(10, 20, ErrorMessage = "Id 只能在 10-20 区间取值")]
        public int Id { get; set; }

        [Required(ErrorMessage = "必填"), MinLength(3, ErrorMessage = "字符串长度不能少于3位")]
        public string Name { get; set; }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            // 还可以解析服务
            var service = validationContext.GetService(typeof(FurionAppService));

            if (Name.StartsWith("Furion"))
            {
                yield return new ValidationResult("不能以Furion开头", new[] { nameof(Name) });
            }
        }

        #region DataValidation特性

        [DataValidation(ValidationTypes.Numeric, ValidationTypes.Integer)]
        public int Cost { get; set; }

        [DataValidation(ValidationPattern.AtLeastOne, ValidationTypes.Chinese, ValidationTypes.Date)]
        public string CostName { get; set; }

        /// <summary>
        /// 可以和Mvc特性共存
        /// </summary>
        [Required, DataValidation(ValidationTypes.Age)]
        public int Age { get; set; }

        [DataValidation(ValidationTypes.IDCard, ErrorMessage = "自定义身份证提示信息")]
        public string IDCard { get; set; }

        /// <summary>
        /// 默认情况下，验证失败信息会根据属性名进行序列化
        /// 但是如果属性序列化自定义了[JsonPropertyName]特性，那么验证失败的消息就不匹配了
        /// 这时我们需要添加[ModelBinder(Name = "序列化对应名字")]进行纠正
        /// </summary>
        [JsonPropertyName("phone_number"), ModelBinder(Name = "phone_number")]
        public string PhoneNumber { get; set; }

        [DataValidation(MyValidationTypes.StrongPassword)]
        public string PassWord { get; set; }

        #endregion

        public string FullName { get; set; }
    }
}
